﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class SteelBar
    {
        #region  计算桩基坐标点
        //立面坐标
        public static List<Point3d> calculatePileSteelElevationCoordinate(Point3d pt, int rebarNum, double radius, double layer,ref List<double> topList)
        {
            double height = 0;
            double width = 0;
            double arcRadius = radius - layer;  
            List<Point3d> list = new List<Point3d>();
            //list.Add(pt.C3D((arcRadius - radius), 0));
            if (rebarNum % 2 != 0)
            {
                for (int i = 1; i < rebarNum; ++i)
                {
                    // 计算圆点
                    double angle = 360 / rebarNum * i;

                    if (angle > 0 && angle < 90)
                    {
                        angle = GeTools.DegreeToRadian(angle);
                        height = Math.Abs(arcRadius * Math.Sin(angle));
                        width = -Math.Abs(arcRadius * Math.Cos(angle));
                    }
                    else if (angle > 90 && angle < 180)
                    {
                        angle = GeTools.DegreeToRadian(180 - angle);
                        height = Math.Abs(arcRadius * Math.Sin(angle));
                        width = Math.Abs(arcRadius * Math.Cos(angle));
                    }
                    //else if (angle > 180 && angle < 270)
                    //{
                    //    angle = GeTools.DegreeToRadian(270 - angle);
                    //    height = -Math.Abs(arcRadius * Math.Cos(angle));
                    //    width = Math.Abs(arcRadius * Math.Sin(angle));
                    //}
                    //else if (angle > 270 && angle < 360)
                    //{
                    //    angle = GeTools.DegreeToRadian(360 - angle);
                    //    height = -Math.Abs(arcRadius * Math.Sin(angle));
                    //    width = -Math.Abs(arcRadius * Math.Cos(angle));
                    //}
                    else if (angle == 0 /*|| angle == 360*/)
                    {
                        height = -arcRadius * Math.Sin(0);
                        width = -arcRadius * Math.Cos(0);
                    }
                    else if (angle == 90)
                    {
                        height = arcRadius * Math.Cos(0);
                        width = arcRadius * Math.Sin(0);
                    }
                    else if (angle == 180)
                    {
                        angle = 360 - angle;
                        height = arcRadius * Math.Sin(0);
                        width = arcRadius * Math.Cos(0);
                    }
                    //else if (angle == 270)
                    //{
                    //    angle = 360 - angle;
                    //    height = -arcRadius * Math.Cos(0);
                    //    width = arcRadius * Math.Sin(0);
                    //}
                    if (angle >= 0 && angle <= 180)
                    {
                        Point3d center = pt.C3D((width), 0);
                        list.Add(center);
                        topList.Add(Math.Abs(width));
                    }
                    
                }      
            }
            else
            {
                double angleV = 360 / rebarNum;
                for (int i = 0; i < rebarNum; ++i)
                {
                    // 计算圆点
                    double angle = angleV/2 + angleV * i;
                    if (angle > 0 && angle < 90)
                    {
                        angle = GeTools.DegreeToRadian(angle);
                        height = Math.Abs(arcRadius * Math.Sin(angle));
                        width = -Math.Abs(arcRadius * Math.Cos(angle));
                    }
                    else if (angle > 90 && angle < 180)
                    {
                        angle = GeTools.DegreeToRadian(180 - angle);
                        height = Math.Abs(arcRadius * Math.Sin(angle));
                        width = Math.Abs(arcRadius * Math.Cos(angle));
                    }
                    //else if (angle > 180 && angle < 270)
                    //{
                    //    angle = GeTools.DegreeToRadian(270 - angle);
                    //    height = -Math.Abs(arcRadius * Math.Cos(angle));
                    //    width = Math.Abs(arcRadius * Math.Sin(angle));
                    //}
                    //else if (angle > 270 && angle < 360)
                    //{
                    //    angle = GeTools.DegreeToRadian(360 - angle);
                    //    height = -Math.Abs(arcRadius * Math.Sin(angle));
                    //    width = -Math.Abs(arcRadius * Math.Cos(angle));
                    //}
                    else if (angle == 0 /*|| angle == 360*/)
                    {
                        height = -arcRadius * Math.Sin(0);
                        width = -arcRadius * Math.Cos(0);
                    }
                    else if (angle == 90)
                    {
                        height = arcRadius * Math.Cos(0);
                        width = arcRadius * Math.Sin(0);
                    }
                    else if (angle == 180)
                    {
                        angle = 360 - angle;
                        height = arcRadius * Math.Sin(0);
                        width = arcRadius * Math.Cos(0);
                    }
                    //else if (angle == 270)
                    //{
                    //    angle = 360 - angle;
                    //    height = -arcRadius * Math.Cos(0);
                    //    width = arcRadius * Math.Sin(0);
                    //}
                    if (angle >= 0 && angle <= 180)
                    {
                        Point3d center = pt.C3D((width), 0);
                        list.Add(center);
                        topList.Add(Math.Abs(width));
                    }
                    
                }
            }

            list.Sort((a, b) => a.X.CompareTo(b.X));
            return list;
        }

        /// <summary>
        ///  平面坐标
        /// </summary>
        /// <param name="pt">参考圆心</param>
        /// <param name="rebarNum">钢筋数量</param>
        /// <param name="radius">半径</param>
        /// <param name="radiusN">钢筋半径</param>
        public static List<Point3d> calculatePileSteelPlanCoordinate(Point3d pt, int rebarNum,double radius,double radiusN)
        {
            List<Point3d> list = new List<Point3d>();
            double height = 0;
            double width = 0;
            double radiusInterval = radius - radiusN;

            for (int i = 0; i < rebarNum; ++i)
            {
                // 计算圆点     
                double angle = 360 / rebarNum * i;

                if (angle > 0 && angle < 90)
                {
                    angle = GeTools.DegreeToRadian(angle);
                    height = Math.Abs(radiusInterval * Math.Sin(angle));
                    width = -Math.Abs(radiusInterval * Math.Cos(angle));
                }
                else if (angle > 90 && angle < 180)
                {
                    angle = GeTools.DegreeToRadian(180 - angle);
                    height = Math.Abs(radiusInterval * Math.Sin(angle));
                    width = Math.Abs(radiusInterval * Math.Cos(angle));
                }
                else if (angle > 180 && angle < 270)
                {
                    angle = GeTools.DegreeToRadian(270 - angle);
                    height = -Math.Abs(radiusInterval * Math.Cos(angle));
                    width = Math.Abs(radiusInterval * Math.Sin(angle));
                }
                else if (angle > 270 && angle < 360)
                {
                    angle = GeTools.DegreeToRadian(360 - angle);
                    height = -Math.Abs(radiusInterval * Math.Sin(angle));
                    width = -Math.Abs(radiusInterval * Math.Cos(angle));
                }
                else if (angle == 0 || angle == 360)
                {
                    height = -radiusInterval * Math.Sin(0);
                    width = -radiusInterval * Math.Cos(0);
                }
                else if (angle == 90)
                {
                    height = radiusInterval * Math.Cos(0);
                    width = radiusInterval * Math.Sin(0);
                }
                else if (angle == 180)
                {
                    angle = 360 - angle;
                    height = radiusInterval * Math.Sin(0);
                    width = radiusInterval * Math.Cos(0);
                }
                else if (angle == 270)
                {
                    angle = 360 - angle;
                    height = -radiusInterval * Math.Cos(0);
                    width = radiusInterval * Math.Sin(0);
                }

                pt.Add(new Vector3d(radius + width, height, 0));
                list.Add(pt);              
            }
            return list;
        }
        #endregion

        #region 桩基钢筋接口

        #region 螺旋筋
        public static void CreateSteelBar(Database db, ref Extents2d ext, Point2d pt, double radius,
 double height, double L, double intervalT = 100, double intervalL = 200, double scale = 100)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region 螺旋钢筋
                // 螺旋钢筋的拉伸间隔
                //double interval1 = 3;
                //double interval2 = 5;
                Point2d ptEnd1 = Point2d.Origin;
                Point2d ptEnd2 = Point2d.Origin;
                Point2d ptEnd = Point2d.Origin;
                List<Point2d> list = new List<Point2d>();
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                Line.AddVertexAt(0, pt.C2D(-radius), 0, 0, 0);
                Line.AddVertexAt(0, pt.C2D(radius), 0, 0, 0);

                list.Add(pt.C2D(-radius));
                list.Add(pt.C2D(radius));

                int num1 = (int)(height / intervalT) + 1;
                for (int i = 0; i < num1; ++i)
                {
                    Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -intervalT / 2);
                    list.Add(pt1.C2D());

                    Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -intervalT / 2);
                    list.Add(pt2.C2D());

                    Line.AddVertexAt(0, pt1, 0, 0, 0);
                    Line.AddVertexAt(0, pt2, 0, 0, 0);
                    if (i == num1 - 1)
                        ptEnd1 = list[list.Count - 1].C2D(radius * 2, -intervalT / 2);
                }

                int num2 = (int)((L - height) / intervalL);
                int j = 1;
                double offsetRadius = radius;
                for (int i = 0; i < num2; ++i)
                {
                    if ((i == num2 - 1) || (i == num2 - 2) || (i == num2 - 3))
                    {
                        double offset = radius * j / 6.0;
                        Line.AddVertexAt(0, list[list.Count - 1].C2D(-radius * 2 + offset, -intervalL / 2), 0, 0, 0);
                        list.Add(list[list.Count - 1].C2D(-radius * 2, -intervalL / 2));
                        Line.AddVertexAt(0, list[list.Count - 1].C2D(radius * 2 - offset, -intervalL / 2), 0, 0, 0);
                        list.Add(list[list.Count - 1].C2D(radius * 2, -intervalL / 2));
                        //offsetRadius = radius * 2 - 6 * offset;
                        ++j;
                    }
                    else
                    {
                        double offset = intervalL * i + intervalL / 2;
                        if (i == 0)
                        {
                            offset = intervalL / 2;
                        }
                        Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -intervalL / 2);
                        list.Add(pt1);
                        Line.AddVertexAt(0, pt1, 0, 0, 0);
                        Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -intervalL / 2);
                        list.Add(pt2);
                        Line.AddVertexAt(0, pt2, 0, 0, 0);
                        //ptEnd2 = pt2;
                    }
                }
                double offsetE = radius * 3 / 6.0;
                Line.AddVertexAt(0, list[list.Count - 1].C2D(-radius * 2 + offsetE), 0, 0, 0);
                list.Add(list[list.Count - 1].C2D(-radius * 2));
                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                #endregion
               
                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="ptEnd">返回结束点</param>
        /// <param name="pt">参考点</param>
        /// <param name="radius">螺旋筋半径</param>
        /// <param name="height">设置螺旋高度</param>
        /// <param name="interval">间隔</param>
        /// <param name="scale">比例</param>
        /// <param name="isHalf">终点位置是否在中间</param>
        public static void CreatePartSteelBar(Database db, ref Extents2d ext,out Point2d ptEnd , Point2d pt, double radius,
double height, double interval = 100, double scale = 100, bool isHalf = false)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region 螺旋钢筋
                // 螺旋钢筋的拉伸间隔             
                List<Point2d> list = new List<Point2d>();
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                Line.AddVertexAt(0, pt.C2D(-radius), 0, 0, 0);
                Line.AddVertexAt(0, pt.C2D(radius), 0, 0, 0);

                list.Add(pt.C2D(-radius));
                list.Add(pt.C2D(radius));
                int num = (int)(height / interval);
                for (int i = 0; i < num; ++i)
                {
                    if (i < num - 1)
                    {
                        Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval * 0.5);
                        list.Add(pt1.C2D());

                        Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval * 0.5);
                        list.Add(pt2.C2D());

                        Line.AddVertexAt(0, pt1, 0, 0, 0);
                        Line.AddVertexAt(0, pt2, 0, 0, 0);
                    }
                    else
                    {
                        Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval * 0.5);
                        list.Add(pt1.C2D());
                        Point2d pt2;
                        if (isHalf)
                            pt2 = list[list.Count - 1].C2D(radius, -interval * 0.5);                      
                        else
                            pt2 = list[list.Count - 1].C2D(radius * 2, -interval * 0.5);
                        list.Add(pt2.C2D());
                        Line.AddVertexAt(0, pt1, 0, 0, 0);
                        Line.AddVertexAt(0, pt2, 0, 0, 0);
                       
                    }
                }

                ptEnd = list[list.Count - 1];
                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                #endregion

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="pt">参考点</param>
        /// <param name="radius">螺旋半径</param>
        /// <param name="height">设置螺旋高度</param>
        /// <param name="interval">间隔</param>
        /// <param name="scale">比例尺</param>
        /// <param name="H">变化大样高度</param>
        /// <param name="BL">变化大样底部宽度</param>
        /// <param name="isHalfTop">起点位置是否在中间</param>
        public static void CreateChangePartSteelBar(Database db, ref Extents2d ext, Point2d pt, double radius,
double height, double interval = 100, double scale = 100, double H = 100, double BL = 10, double intervalChange = 150, bool isHalfTop=true)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region 螺旋钢筋
                // 螺旋钢筋的拉伸间隔             
                List<Point2d> list = new List<Point2d>();
                Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"]  };
                if (isHalfTop)
                {
                    Line.AddVertexAt(0, pt, 0, 0, 0);
                    list.Add(pt);
                    Line.AddVertexAt(0, pt.C2D(radius), 0, 0, 0);
                    list.Add(pt.C2D(radius));
                }
                else
                {
                    Line.AddVertexAt(0, pt.C2D(-radius), 0, 0, 0);
                    Line.AddVertexAt(0, pt.C2D(radius), 0, 0, 0);

                    list.Add(pt.C2D(-radius));
                    list.Add(pt.C2D(radius));
                }
                int num = (int)((height-H) / interval);
                int changeNum =(int) (H / intervalChange);
                num = num + changeNum;
                if (changeNum > num)
                    return;
                int j = 1;
                bool isOddLast = false;
                if (H - changeNum * intervalChange > 0)
                {
                    num = num + 1;
                    isOddLast=true;
                }
                for (int i = 0; i < num; ++i)
                {
                    int count = num - changeNum;
                    if (isOddLast)
                        count = num - changeNum-1;
                    if (i < count) //变化前螺旋绘制
                    {
                        Point2d pt1 = list[list.Count - 1].C2D(-radius * 2, -interval *0.5);
                        list.Add(pt1.C2D());

                        Point2d pt2 = list[list.Count - 1].C2D(radius * 2, -interval * 0.5);
                        list.Add(pt2.C2D());

                        Line.AddVertexAt(0, pt1, 0, 0, 0);
                        Line.AddVertexAt(0, pt2, 0, 0, 0);
                    }
                    else  //变化螺旋绘制
                    {
                        double offset = intervalChange * j* BL / H;
                        int countC = num;
                        if (isOddLast)
                            countC = num - 1;
                        if (i < countC)
                        {
                            Line.AddVertexAt(0, list[list.Count - 1].C2D(-radius * 2 + offset, -intervalChange * 0.5), 0, 0, 0);
                            list.Add(list[list.Count - 1].C2D(-radius * 2, -intervalChange * 0.5));
                            Line.AddVertexAt(0, list[list.Count - 1].C2D(radius * 2 - offset, -intervalChange * 0.5), 0, 0, 0);
                            list.Add(list[list.Count - 1].C2D(radius * 2, -intervalChange * 0.5));
                        }
                        else
                        {
                            offset = BL;
                            Line.AddVertexAt(0, list[list.Count - 1].C2D(-radius * 2 + offset, -(H-intervalChange* changeNum)), 0, 0, 0);
                            list.Add(list[list.Count - 1].C2D(-radius * 2, -(H - intervalChange * changeNum)));
                            Line.AddVertexAt(0, list[list.Count - 1].C2D(radius * 2 - offset, 0), 0, 0, 0);
                            list.Add(list[list.Count - 1].C2D(radius * 2, 0));
                        }
                        ++j;
                    }

                }

                ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                res.Add(Line);
                #endregion

                foreach (DBObject drawitem in res)
                {
                    recorder.AppendEntity((Entity)drawitem);
                    tr.AddNewlyCreatedDBObject(drawitem, true);
                }

                tr.Commit();

            }
        }
        #endregion

        #region 立面
        public static void DrawUpperRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, double radius, double Height = 1200, double interval = 150, double H = 100, double BL = 10)
        {
            double tH = 20 * 10, tH1 = 50 * 10, tH2 = 200 * 10;
            #region 
            int num = (int)(Height / interval);
            #region 网格绘制
            for (int i = 0; i < ptTop.Count; i++)
            {
                double offset =  (num-1)* interval * BL/ H ;
                offset = Math.Abs(offset * (i - ptTop.Count * 0.5) / (ptTop.Count * 0.5));
                Point3d ptT;
                if (ptTop[i].X> 0)
                {
                    ptT = ptTop[i].C3D(offset, Height + tH);
                }
                else
                {
                    ptT = ptTop[i].C3D(-offset, Height + tH);
                }
                PLPloter.AddLine(db,ptTop[i].C2D(0, tH), ptT.C2D(), ref ext, "H粗线");
            }
            for (int i = 0; i < num+1; i++)
            {
                double offset = interval *i* BL / H ;
                PLPloter.AddLine(db, pt.C2D(-radius- offset, interval * i+ tH), pt.C2D(radius+offset, interval * i+ tH), ref ext, "H粗线");
            }
            #endregion
            #endregion

        }
        public static void DrawRebarGrid(Database db, ref Extents2d ext,Point2d pt, List<Point3d> ptTop,double radius, double Height=5000,double interval = 100,double arcRadius = 1000)
        {
            double tH = 20*10, tH1 = 50 * 10, tH2 = 200 * 10;
            #region 
            int num = (int)(Height / interval);
            #region  特殊处理
            PLPloter.AddLine(db, pt.C2D(-radius, tH), pt.C2D(radius, tH), ref ext, "H粗线");
            PLPloter.AddLine(db,pt.C2D(-radius, tH/2), pt.C2D(radius, tH/2), ref ext, "H粗线");
            #endregion
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                #region  圆弧

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                double arcAngle = 60;

                Arc arc1 = new Arc(pt.C3D(-arcRadius+arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)), -Height + arcRadius  * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius , GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);

                Arc arc2 = new Arc(pt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))) , -(Height + arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))) ),
                    arcRadius , GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);

                Arc arc3 = new Arc(pt.C3D(( arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))) , -(Height - arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))) ),
                    arcRadius , GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle))
                { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);
                #endregion
                #region 网格绘制
                Point3dCollection pts = new Point3dCollection();
                for (int i = 0; i < ptTop.Count; i++)
                {
                    pts.Clear();
                    Polyline line = new Polyline() { Closed = false, LayerId = lt["H粗线"]};
                    // 计算垂直钢筋线与弧线的交点
                    if (ptTop[i].X <= arc1.Center.X + arcRadius  * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {
                        Line line1 = new Line(ptTop[i].C3D(0, tH), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                        MathExtensions.LineIntersectArc(line1, arc1,ref node);

                        line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);
                        if (ptTop[i].X <= arc1.Center.X)
                        {
                            //line.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                            line.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                        }
                        else if (arc1.Center.X <= ptTop[i].X && ptTop[i].X <= arc1.Center.X + arcRadius  * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                        {
                            //line.AddVertexAt(0, pts[1].C2D(), 0, 0, 0);
                            line.AddVertexAt(0, node[1].C2D(), 0, 0, 0);
                        }
                    }
                    else if (arc3.Center.X - arcRadius  * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)) <= ptTop[i].X && ptTop[i].X <= arc3.Center.X + arcRadius  * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                    {
                    
                        Line line1 = new Line(ptTop[i].C3D(), ptTop[i].C3D(0, -Height * 2));
                        List<Point3d> node = new List<Point3d>();
                        MathExtensions.LineIntersectArc(line1, arc2, ref node);
                        line.AddVertexAt(0, ptTop[i].C2D(0, tH), 0, 0, 0);
                        if (ptTop[i].X <= arc3.Center.X)
                        {
                            //line.AddVertexAt(0, pts[1].C2D(), 0, 0, 0);
                            line.AddVertexAt(0, node[1].C2D(), 0, 0, 0);
                        }
                        else if (ptTop[i].X >= arc3.Center.X && arc3.Center.X <= arc3.Center.X + arcRadius  * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)))
                        {
                            //line.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                            line.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                        }
                    }
                    recorder.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    //PolylinePloter.AddLine(db,db, ref ext, ptTop[i].C3D(0, tH), ptTop[i].C3D(0, -Height), "粗线", 3);
                }

             
                for (int i = 0; i < num; i++)
                {
                    pts.Clear();
                   
                    Line line1 = new Line(pt.C3D(-radius, -interval * i), pt.C3D(radius, -interval * i));
                    List<Point3d> node = new List<Point3d>();
                    MathExtensions.LineIntersectArc(line1, arc2, ref node);
                    if (node.Count>0)
                    {
                        Polyline line2 = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                        Polyline line3 = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                        if (node[0].X > node[1].X)
                        {
                            line2.AddVertexAt(0, pt.C2D(-radius, -i * interval), 0, 0, 0);
                            line2.AddVertexAt(1, node[1].C2D(), 0, 0, 0);
                            line3.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                            line3.AddVertexAt(1, pt.C2D(radius, -i * interval), 0, 0, 0);
                        }
                        else
                        {
                            line2.AddVertexAt(0, pt.C2D(-radius, -i * interval), 0, 0, 0);
                            line2.AddVertexAt(1, node[0].C2D(), 0, 0, 0);
                            line3.AddVertexAt(0, node[1].C2D(), 0, 0, 0);
                            line3.AddVertexAt(1, pt.C2D(radius, -i * interval), 0, 0, 0);
                        }
                        recorder.AppendEntity(line2);
                        tr.AddNewlyCreatedDBObject(line2, true);
                        recorder.AppendEntity(line3);
                        tr.AddNewlyCreatedDBObject(line3, true);
                    }
                    else
                       PLPloter.AddLine(db, pt.C2D(-radius, -interval * i), pt.C2D(radius, -interval * i), ref ext, "H粗线");
                }
                #endregion

                tr.Commit();
            }
            #endregion

        }

        public static void DrawBottomRebarGrid(Database db, ref Extents2d ext, Point2d pt, List<Point3d> ptTop, double radius, double Height = 1200, double interval = 150, double H = 100, double BL = 10, bool isDrawAll = true, int drawNum = 0, double arcRadius = 1000)
        {
            #region 
            int num = (int)(Height / interval);
            int changeNum = (int)(H / BL);
            if (num < changeNum)
                return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                Point3dCollection pts = new Point3dCollection();
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                #region  圆弧
                double arcAngle = 60;
                double cosH = arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2));
                // 绘制弧线
                Arc arc1 = new Arc(pt.C3D(-arcRadius+arcRadius / 2 , -cosH ), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle)) { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc1.Bounds.Value.MinPoint.C2D(), arc1.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);

                Arc arc2 = new Arc(pt.C3D((arcRadius / 2) , -cosH ), arcRadius, GeTools.DegreeToRadian(60), GeTools.DegreeToRadian(60 + arcAngle)) { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc2.Bounds.Value.MinPoint.C2D(), arc2.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc2);
                tr.AddNewlyCreatedDBObject(arc2, true);

                Arc arc3 = new Arc(pt.C3D(-arcRadius+arcRadius / 2 , cosH ), arcRadius, GeTools.DegreeToRadian(240), GeTools.DegreeToRadian(240 + arcAngle)) { LayerId = lt["H细线"] };
                ext = ext.Add(new Extents2d(arc3.Bounds.Value.MinPoint.C2D(), arc3.Bounds.Value.MaxPoint.C2D()));
                recorder.AppendEntity(arc3);
                tr.AddNewlyCreatedDBObject(arc3, true);
                #endregion

                #region 网格绘制
                int n = 0;
                for (int m = 0; m < num; m++)
                {

                    if (m < num - changeNum)
                    {
                        for (int i = 0; i < ptTop.Count; i++)
                        {
                            pts.Clear();
                            Polyline line = new Polyline() { Closed = false, LayerId = lt["H粗线"]};
                          
                            if (ptTop[i].X <= pt.X)
                            {
                                //Polyline l1 = new Polyline();
                                //l1.AddVertexAt(0, ptTop[i].C2D(0,Height), 0, 0, 0);
                                //l1.AddVertexAt(0, ptTop[i].C2D(0, -Height * 2), 0, 0, 0);
                                //l1.IntersectWith(arc3, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero);
                                //if (pts.Count > 0)
                                //{
                                //    line.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                                //    line.AddVertexAt(1, ptTop[i].C2D(0, -Height + H), 0, 0, 0);
                                //}
                                //else
                                //    PLPloter.AddLine(db,ptTop[i].C2D(0, 0), ptTop[i].C2D(0, -Height + H), ref ext, "H粗线");
                                Line line1 = new Line(ptTop[i].C3D(0, Height), ptTop[i].C3D(0, -Height * 2));
                                List<Point3d> node = new List<Point3d>();
                                MathExtensions.LineIntersectArc(line1, arc3, ref node);
                                if (node.Count > 0)
                                {
                                    line.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                                    line.AddVertexAt(1, ptTop[i].C2D(0, -Height + H), 0, 0, 0);
                                }
                                else
                                    PLPloter.AddLine(db, ptTop[i].C2D(0, 0), ptTop[i].C2D(0, -Height + H), ref ext, "H粗线");

                            }
                            else 
                            {
                                //Polyline l1 = new Polyline();
                                //l1.AddVertexAt(0, ptTop[i].C2D(0, Height), 0, 0, 0);
                                //l1.AddVertexAt(0, ptTop[i].C2D(0, -Height * 2), 0, 0, 0);
                                //l1.IntersectWith((Entity)arc2, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero);
                                //if (pts.Count > 0)
                                //{
                                //    line.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                                //    line.AddVertexAt(1, ptTop[i].C2D(0, -Height + H), 0, 0, 0);
                                //}
                                //else
                                //    PLPloter.AddLine(db, ptTop[i].C2D(0, 0), ptTop[i].C2D(0, -Height + H), ref ext, "H粗线");

                                Line line1 = new Line(ptTop[i].C3D(0, Height), ptTop[i].C3D(0, -Height * 2));
                                List<Point3d> node = new List<Point3d>();
                                MathExtensions.LineIntersectArc(line1, arc2, ref node);
                                if (node.Count > 0)
                                {
                                    line.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                                    line.AddVertexAt(1, ptTop[i].C2D(0, -Height + H), 0, 0, 0);
                                }
                                else
                                    PLPloter.AddLine(db,ptTop[i].C2D(0, 0), ptTop[i].C2D(0, -Height + H), ref ext, "H粗线");
                            }
                            recorder.AppendEntity(line);
                            tr.AddNewlyCreatedDBObject(line, true);
                            //PolylinePloter.AddLine(db,db, ref ext, ptTop[i].C3D(0, 0), ptTop[i].C3D(0, -Height + H), "粗线", 3);
                        }
                        if (isDrawAll)
                        {
                            //pts.Clear();
                            //Polyline l1 = new Polyline();
                            //l1.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                            //l1.AddVertexAt(0, pt.C2D(radius, -interval * m), 0, 0, 0);
                            //l1.IntersectWith((Entity)arc3, Intersect.ExtendBoth, pts, IntPtr.Zero, IntPtr.Zero);
                            //if (pts.Count > 0)
                            //{
                            //    Polyline line2 = new Polyline() { Closed = false, LayerId = lt["H粗线"], ColorIndex = 3 };
                            //    Polyline line3 = new Polyline() { Closed = false, LayerId = lt["H粗线"], ColorIndex = 3 };
                            //    if (pts[0].X > pts[1].X)
                            //    {

                            //        if (pts[1].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                            //        {
                            //            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                            //            line2.AddVertexAt(1, pts[1].C2D(), 0, 0, 0);
                            //        }
                            //        line3.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                            //        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                            //    }
                            //    else
                            //    {
                            //        if (pts[0].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                            //        {
                            //            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                            //            line2.AddVertexAt(1, pts[0].C2D(), 0, 0, 0);
                            //        }
                            //        line3.AddVertexAt(0, pts[1].C2D(), 0, 0, 0);
                            //        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                            //    }
                            //    recorder.AppendEntity(line2);
                            //    tr.AddNewlyCreatedDBObject(line2, true);
                            //    recorder.AppendEntity(line3);
                            //    tr.AddNewlyCreatedDBObject(line3, true);
                            //}
                            //else
                            //    PLPloter.AddLine(db, pt.C2D(-radius, -interval * m), pt.C2D(radius, -interval * m), ref ext, "H粗线");

                  
                            Line line1 = new Line(pt.C3D(-radius, -interval * m), pt.C3D(radius, -interval * m));
                            List<Point3d> node = new List<Point3d>();
                            MathExtensions.LineIntersectArc(line1, arc3, ref node);
                            if (node.Count > 0)
                            {
                                Polyline line2 = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                                Polyline line3 = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                                if (node[0].X > node[1].X)
                                {

                                    if (node[1].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                    {
                                        line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                        line2.AddVertexAt(1, node[1].C2D(), 0, 0, 0);
                                    }
                                    line3.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                                    line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                }
                                else
                                {
                                    if (node[0].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                    {
                                        line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                        line2.AddVertexAt(1, node[0].C2D(), 0, 0, 0);
                                    }
                                    line3.AddVertexAt(0, node[1].C2D(), 0, 0, 0);
                                    line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                }
                                recorder.AppendEntity(line2);
                                tr.AddNewlyCreatedDBObject(line2, true);
                                recorder.AppendEntity(line3);
                                tr.AddNewlyCreatedDBObject(line3, true);
                            }
                            else
                                PLPloter.AddLine(db,pt.C2D(-radius, -interval * m), pt.C2D(radius, -interval * m), ref ext, "H粗线");
                        }
                        else
                        {
                            if (m < drawNum)
                            {
                                //pts.Clear();
                                //Polyline l1 = new Polyline();
                                //l1.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                //l1.AddVertexAt(0, pt.C2D(radius, -interval * m), 0, 0, 0);
                                //l1.IntersectWith((Entity)arc3, Intersect.ExtendBoth, pts, IntPtr.Zero, IntPtr.Zero);
                                //if (pts.Count > 0)
                                //{
                                //    Polyline line2 = new Polyline() { Closed = false, LayerId = lt["H粗线"], ColorIndex = 3 };
                                //    Polyline line3 = new Polyline() { Closed = false, LayerId = lt["H粗线"], ColorIndex = 3 };
                                //    if (pts[0].X > pts[1].X)
                                //    {

                                //        if (pts[1].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                //        {
                                //            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                //            line2.AddVertexAt(1, pts[1].C2D(), 0, 0, 0);
                                //        }
                                //        line3.AddVertexAt(0, pts[0].C2D(), 0, 0, 0);
                                //        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                //    }
                                //    else
                                //    {
                                //        if (pts[0].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                //        {
                                //            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                //            line2.AddVertexAt(1, pts[0].C2D(), 0, 0, 0);
                                //        }
                                //        line3.AddVertexAt(0, pts[1].C2D(), 0, 0, 0);
                                //        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                //    }
                                //    recorder.AppendEntity(line2);
                                //    tr.AddNewlyCreatedDBObject(line2, true);
                                //    recorder.AppendEntity(line3);
                                //    tr.AddNewlyCreatedDBObject(line3, true);
                                //}
                                //else
                                //    PLPloter.AddLine(db, pt.C2D(-radius, -interval * m), pt.C2D(radius, -interval * m), ref ext, "H粗线");

                                //pts.Clear();
                                //Polyline l1 = new Polyline();
                                //l1.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                //l1.AddVertexAt(0, pt.C2D(radius, -interval * m), 0, 0, 0);
                                //l1.IntersectWith((Entity)arc3, Intersect.ExtendBoth, pts, IntPtr.Zero, IntPtr.Zero);
                                Line line1 = new Line(pt.C3D(-radius, -interval * m), pt.C3D(radius, -interval * m));
                                List<Point3d> node = new List<Point3d>();
                                MathExtensions.LineIntersectArc(line1, arc3, ref node);
                                if (node.Count > 0)
                                {
                                    Polyline line2 = new Polyline() { Closed = false, LayerId = lt["H粗线"]};
                                    Polyline line3 = new Polyline() { Closed = false, LayerId = lt["H粗线"]};
                                    if (node[0].X > node[1].X)
                                    {

                                        if (node[1].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                        {
                                            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                            line2.AddVertexAt(1, node[1].C2D(), 0, 0, 0);
                                        }
                                        line3.AddVertexAt(0, node[0].C2D(), 0, 0, 0);
                                        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                    }
                                    else
                                    {
                                        if (node[0].X - pt.C2D(-arcRadius, -interval * m).X > 5)
                                        {
                                            line2.AddVertexAt(0, pt.C2D(-radius, -interval * m), 0, 0, 0);
                                            line2.AddVertexAt(1, node[0].C2D(), 0, 0, 0);
                                        }
                                        line3.AddVertexAt(0, node[1].C2D(), 0, 0, 0);
                                        line3.AddVertexAt(1, pt.C2D(radius, -interval * m), 0, 0, 0);
                                    }
                                    recorder.AppendEntity(line2);
                                    tr.AddNewlyCreatedDBObject(line2, true);
                                    recorder.AppendEntity(line3);
                                    tr.AddNewlyCreatedDBObject(line3, true);
                                }
                                else
                                    PLPloter.AddLine(db,pt.C2D(-radius, -interval * m), pt.C2D(radius, -interval * m), ref ext, "H粗线");
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ptTop.Count; i++)
                        {
                            double offset = (changeNum - 1) * interval * BL / H;
                            offset = Math.Abs(offset * (i - ptTop.Count * 0.5) / (ptTop.Count * 0.5));
                            Point3d ptT;
                            if (ptTop[i].X > 0)
                            {
                                ptT = ptTop[i].C3D(-offset, -Height);
                            }
                            else
                            {
                                ptT = ptTop[i].C3D(offset, -Height);
                            }

                            PLPloter.AddLine(db, ptTop[i].C2D(0, -Height + H), ptT.C2D(), ref ext, "H粗线");
                        }

                        double offset0 = interval * n * BL / H;
                        if (isDrawAll)
                            PLPloter.AddLine(db,pt.C2D(-radius + offset0, -interval * m), pt.C2D(radius - offset0, -interval * m), ref ext, "H粗线");
                        else
                        {
                            if (m < drawNum)
                            {                                
                                PLPloter.AddLine(db, pt.C2D(-radius + offset0, -interval * m), pt.C2D(radius - offset0, -interval * m), ref ext, "H粗线");
                            }
                        }
                        n++;
                    }
                }
                #endregion           
                tr.Commit();
            }
            #endregion
        }
        #endregion

        #region 大样
        public static void DrawDetails(Database db, ref Extents2d ext, double scale, Point2d pt, Point2d ptB, double TopH, double topL, double BottomH,double BottomL)
        {
            RebarPLPloter.CreateBreakLine(db, ref ext, pt.C2D(0, 20 * 10), ptB, scale);

            Line line = PLPloter.AddLine(db,pt.C2D(0, 20 * 10 + TopH), pt.C2D(0, 20 * 10), ref ext, "H钢筋");
            Line lineTop = PLPloter.AddLine(db,pt.C2D(-topL, 20 * 10 + TopH), pt.C2D(0, 20 * 10), ref ext, "H钢筋");
            PLPloter.AddLine(db,ptB.C2D(), ptB.C2D(BottomL,-BottomH), ref ext, "H钢筋");
            DimPloter.DimRot(db,pt.C3D(0, 20 * 10), pt.C3D(-topL, 20 * 10 + TopH), pt.C3D(-topL-3*scale,0), ref ext,90, scale, "",Unit.Millimeter,Unit.Centimeter);
            DimPloter.DimRot(db, pt.C3D(0, 20 * 10), pt.C3D(-topL, 20 * 10 + TopH), pt.C3D(0, 20 * 10 + TopH+3*scale), ref ext, 0, scale, "", Unit.Millimeter, Unit.Centimeter);

            DimPloter.DimRot(db, ptB.C3D(), ptB.C3D(BottomL, -BottomH), ptB.C3D(- 3 * scale, 0), ref ext, 90, scale, "", Unit.Millimeter, Unit.Centimeter);
            DimPloter.DimRot(db, ptB.C3D(), ptB.C3D(BottomL, -BottomH), ptB.C3D(0, -3 * scale - BottomH), ref ext, 0, scale, "", Unit.Millimeter, Unit.Centimeter);
            line =new Line(pt.C3D(0, 20 * 10 + TopH), pt.C3D(0, 20 * 10));
            lineTop = new Line(pt.C3D(-topL, 20 * 10 + TopH), pt.C3D(0, 20 * 10));
            //DimPloter.DimAng(db,line, lineTop, pt.C3D(0, 20 * 10 + TopH-3.5* scale), ref ext, scale);
        }
        #endregion

        #endregion
    }

}
